package org.emet.jee.core.dao;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Set;

import org.emet.jee.console.dao.sys.MaintLogDao;
import org.emet.jee.console.entity.BaseEntity;
import org.emet.jee.console.entity.sys.MaintLog;
import org.emet.jee.core.utils.spring.SpringContextHolder;
import org.hibernate.HibernateException;
import org.hibernate.event.DeleteEvent;
import org.hibernate.event.DeleteEventListener;
import org.hibernate.event.SaveOrUpdateEvent;
import org.hibernate.event.SaveOrUpdateEventListener;

public class SaveOrUpdateListener implements SaveOrUpdateEventListener, DeleteEventListener {

	private static final long serialVersionUID = 1L;

	public void onSaveOrUpdate(SaveOrUpdateEvent evt) throws HibernateException {
		Object entity = evt.getObject();
		if (entity instanceof BaseEntity) {
			((BaseEntity) entity).setTimeStamp(new Timestamp(new Date().getTime()));
		}
	}

	public void onDelete(DeleteEvent paramDeleteEvent) throws HibernateException {
		MaintLogDao dao = (MaintLogDao) SpringContextHolder.getBean("maintLogDao");
		Object entity = paramDeleteEvent.getObject();
		MaintLog maintLog = new MaintLog();
		maintLog.setEntity(entity.getClass().getName());
		maintLog.setOperation("DELETE");
		dao.save(maintLog);
	}

	public void onDelete(DeleteEvent paramDeleteEvent, Set paramSet) throws HibernateException {
		// TODO Auto-generated method stub

	}

}
